package cn.damong.response;

import java.io.Serializable;

/**
 * 分页数据对象
 * 
 * @author CodingPanda
 * @2019年5月15日 下午10:33:00
 *
 */
public class Page implements Serializable{
    private static final long serialVersionUID = -3071149685377205927L;

    /**
     * 总记录数
     */
	private Long totelCount;

	/**
	 * 当前页码，从1开始
	 */
	private Integer currentPage;

	/**
	 * 每页数据条数
	 */
	private Integer pageSize;

	/**
	 * 分页起始点
	 */
	private Long start;

	/**
	 * 页数
	 */
	private Integer pageCount;

	public Page() {
	    
	}
	
	public Page(Long totelCount, Integer currentPage, Integer pageSize) {
		this.totelCount = totelCount;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}
	
	public Page(Long totelCount, Integer currentPage, Integer pageSize, Long start) {
		this.totelCount = totelCount;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.start = start;
	}
	
	public Page(Long totelCount, Integer currentPage, Integer pageSize, Long start, Integer pageCount) {
		this.totelCount = totelCount;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.start = start;
		this.pageCount = pageCount;
	}
	
	/**
	 * @param page 页面，从1开始
	 * @param pageSize 每页记录条数
	 * @param totalCount 总记录数
	 * @return
	 */
	public static Page initPage(Integer page, Integer pageSize, Long totalCount){
		if(null == page || 0 >= page){
			page = 1;
		}
		if(null == pageSize || 0 >= pageSize){
			pageSize = 20;
		}
		Long start = 1l * (page - 1) * pageSize;
		Long pageCount = (totalCount + pageSize - 1) / pageSize;
		return new Page(totalCount, page, pageSize, start, pageCount.intValue());
	}

	public Long getTotelCount() {
		return totelCount;
	}

	public void setTotelCount(Long totelCount) {
		this.totelCount = totelCount;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Long getStart() {
		return start;
	}

	public void setStart(Long start) {
		this.start = start;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((currentPage == null) ? 0 : currentPage.hashCode());
        result = prime * result + ((pageCount == null) ? 0 : pageCount.hashCode());
        result = prime * result + ((pageSize == null) ? 0 : pageSize.hashCode());
        result = prime * result + ((start == null) ? 0 : start.hashCode());
        result = prime * result + ((totelCount == null) ? 0 : totelCount.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Page other = (Page) obj;
        if (currentPage == null) {
            if (other.currentPage != null) {
                return false;
            }
        } else if (!currentPage.equals(other.currentPage)) {
            return false;
        }
        if (pageCount == null) {
            if (other.pageCount != null) {
                return false;
            }
        } else if (!pageCount.equals(other.pageCount)) {
            return false;
        }
        if (pageSize == null) {
            if (other.pageSize != null) {
                return false;
            }
        } else if (!pageSize.equals(other.pageSize)) {
            return false;
        }
        if (start == null) {
            if (other.start != null) {
                return false;
            }
        } else if (!start.equals(other.start)) {
            return false;
        }
        if (totelCount == null) {
            if (other.totelCount != null) {
                return false;
            }
        } else if (!totelCount.equals(other.totelCount)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Page [totelCount=" + totelCount + ", currentPage=" + currentPage + ", pageSize=" + pageSize + ", start="
                + start + ", pageCount=" + pageCount + "]";
    }

}
